/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package questionariosgui;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Felipe
 */public class QuestionariosGUI extends javax.swing.JFrame {
     
     private Questionario list = new Questionario();
     
     private Questao recebeInterface() {
           Questao q = new Questao();
           q.setPergunta(campoQuestao.getText());
           if (tipoDiscursiva.isSelected()) q.TipoDiscursiva();
           if (tipoObjetiva.isSelected()) q.TipoObjetiva();
           return q;
        }
    /**
     * Creates new form QuestionariosGUI
     */
    public QuestionariosGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        groupTipo = new javax.swing.ButtonGroup();
        labelQuestao = new javax.swing.JLabel();
        campoQuestao = new javax.swing.JTextField();
        buttonGera = new javax.swing.JButton();
        labelTipo = new javax.swing.JLabel();
        buttonInsere = new javax.swing.JButton();
        labelIndice = new javax.swing.JLabel();
        boxIndice = new javax.swing.JComboBox();
        buttonRemove = new javax.swing.JButton();
        tipoDiscursiva = new javax.swing.JRadioButton();
        tipoObjetiva = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoQuestionario = new javax.swing.JTextArea();
        labelQuestionario = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelQuestao.setText("Questão: ");

        buttonGera.setText("Gerar Questão");
        buttonGera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGeraActionPerformed(evt);
            }
        });

        labelTipo.setText("Tipo:");

        buttonInsere.setText("Inserir Questão");
        buttonInsere.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonInsereActionPerformed(evt);
            }
        });

        labelIndice.setText("Índice:");

        buttonRemove.setText("Remover Questão");
        buttonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoveActionPerformed(evt);
            }
        });

        groupTipo.add(tipoDiscursiva);
        tipoDiscursiva.setText("Discursiva");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, groupTipo, org.jdesktop.beansbinding.ELProperty.create("${selection.group}"), tipoDiscursiva, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        groupTipo.add(tipoObjetiva);
        tipoObjetiva.setText("Objetiva");
        tipoObjetiva.setActionCommand("");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, groupTipo, org.jdesktop.beansbinding.ELProperty.create("${selection.group}"), tipoObjetiva, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        campoQuestionario.setColumns(20);
        campoQuestionario.setRows(5);
        campoQuestionario.setToolTipText("");
        jScrollPane1.setViewportView(campoQuestionario);

        labelQuestionario.setText("Questionário:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(labelIndice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelTipo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(labelQuestao, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tipoDiscursiva)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                                .addComponent(tipoObjetiva))
                            .addComponent(boxIndice, javax.swing.GroupLayout.Alignment.LEADING, 0, 229, Short.MAX_VALUE)
                            .addComponent(campoQuestao, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(buttonInsere, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(buttonGera, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(buttonRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelQuestionario)
                .addGap(176, 176, 176))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(campoQuestao, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addComponent(buttonGera)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tipoDiscursiva)
                    .addComponent(tipoObjetiva)
                    .addComponent(buttonInsere))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelIndice)
                    .addComponent(boxIndice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRemove))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelQuestionario, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonInsereActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInsereActionPerformed
        tipoDiscursiva.setEnabled(true);
        tipoObjetiva.setEnabled(true);
        Questao q = recebeInterface();
        list.setQuestionario(q.getPergunta(), q.getTipo());
        if(list.sizeQuestionario() > 0) boxIndice.addItem(list.sizeQuestionario());
        String str = list.mostraQuestionario();
        campoQuestionario.setText(str);
        campoQuestao.setText("");
    }//GEN-LAST:event_buttonInsereActionPerformed

    private void buttonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
        int tamanho, index;
        tamanho = boxIndice.getItemCount();
        index = boxIndice.getSelectedIndex();
        if (tamanho == 0) return;
        boxIndice.removeItem(index+1);
        list.removeQuestionario(index);
        for (int i = 1; i < tamanho+1; i++) {
            boxIndice.removeItem(i);
        }
        for (int i = 1; i <= list.sizeQuestionario(); i++) {
            boxIndice.addItem(i);
        }
        String str = list.mostraQuestionario();
        campoQuestionario.setText(str);
    }//GEN-LAST:event_buttonRemoveActionPerformed

    private void buttonGeraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGeraActionPerformed
        Questao qGerada = new Questao();
        qGerada.geraQuestao();
        campoQuestao.setText(qGerada.getPergunta());
        if (qGerada.getTipo() == 0) {
            tipoDiscursiva.setSelected(rootPaneCheckingEnabled);
        }
        if (qGerada.getTipo() == 1) {
            tipoObjetiva.setSelected(rootPaneCheckingEnabled);
        }
        tipoDiscursiva.setEnabled(false);
        tipoObjetiva.setEnabled(false);
    }//GEN-LAST:event_buttonGeraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QuestionariosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuestionariosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuestionariosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuestionariosGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuestionariosGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox boxIndice;
    private javax.swing.JButton buttonGera;
    private javax.swing.JButton buttonInsere;
    private javax.swing.JButton buttonRemove;
    private javax.swing.JTextField campoQuestao;
    private javax.swing.JTextArea campoQuestionario;
    private javax.swing.ButtonGroup groupTipo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelIndice;
    private javax.swing.JLabel labelQuestao;
    private javax.swing.JLabel labelQuestionario;
    private javax.swing.JLabel labelTipo;
    private javax.swing.JRadioButton tipoDiscursiva;
    private javax.swing.JRadioButton tipoObjetiva;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
